package com.hd123.rumba.gwt.widget.client.codenamebox;

import java.util.ArrayList;
import java.util.Iterator;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ChangeListenerCollection;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SourcesChangeEvents;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.hd123.rumba.gwt.widget.client.Util;

/**
 * <li>输入框中ENTER,查询. 输入框中按F3或点击浏览图标, 显示浏览框.
 * <li>在浏览框中可使用UP,DOWN,HOME,END,PAGEUP,PAGEDOWN,CTRL+PAGEUP,CTRL+PAGEDOWN,ENTER,ESCAPE
 * <li>在浏览框的代码输入框中输入内容后,ENTER,列出包含代码的记录(名称输入框中的内容不作为查询条件).
 * <li>在浏览框的名称输入框中输入内容后,ENTER,列出包含名称的记录(代码输入框中的内容不作为查询条件).
 * 
 * @author caili
 * 
 */
public class CodeNameBox extends HorizontalPanel implements KeyboardListener, ClickListener,
    SourcesChangeEvents, ChangeListener {
  protected String userDataUuid;
  protected TextBox codeBox;
  protected Image searchButton;
  protected Label nameLabel;

  public CodeNameBox() {
    super();
    setSpacing(0);
    add(codeBox = new TextBox());
    add(nameLabel = new Label());
    nameLabel.setWordWrap(false);
    setCellVerticalAlignment(nameLabel, HorizontalPanel.ALIGN_MIDDLE);
    add(searchButton = new Image("images/search_small.gif"));
    codeBox.addKeyboardListener(this);
    codeBox.addChangeListener(this);
    searchButton.addClickListener(this);
  }

  public CodeNameBox(UuidCodeName ucn) {
    this();
    setUuidCodeName(ucn);
  }

  public void setWidth(String width) {
    super.setWidth(width);
  }

  public UuidCodeName getUuidCodeName() {
    return new UuidCodeName(userDataUuid, codeBox.getText(), nameLabel.getText());
  }

  public void setUuidCodeName(UuidCodeName ucn) {
    if (ucn == null) {
      setUserDataUuid(null);
      setCode(null);
      setName(null);
    } else {
      setUserDataUuid(ucn.getUuid());
      setCode(ucn.getCode());
      setName(ucn.getName());
    }
  }

  public String getUserDataUuid() {
    return userDataUuid;
  }

  public void setUserDataUuid(String userData) {
    this.userDataUuid = userData;
  }

  public String getCode() {
    return codeBox.getText();
  }

  public void setCode(String code) {
    codeBox.setText(code);
  }

  public String getName() {
    return nameLabel.getText();
  }

  public void setName(String name) {
    nameLabel.setText(name);
    nameLabel.setStyleName("jdgwt-cnb-name");
  }

  public void setNameErrorText(String text) {
    // nameLabel.setText(text);
    DOM.setInnerHTML(nameLabel.getElement(), text);
    nameLabel.setStyleName("jdgwt-cnb-name-error");
  }

  public Label getNameLabel() {
    return nameLabel;
  }

  public TextBox getCodeBox() {
    return codeBox;
  }

  public Image getSearchButton() {
    return searchButton;
  }

  public void focus() {
    Util.focusTextBox(getCodeBox());
  }

  // KeyboardListener for code box
  public void onKeyDown(Widget sender, char keyCode, int modifiers) {
    if (sender == codeBox) {
      if (keyCode == KEY_ENTER) {
        codeBox.setKey((char) KEY_TAB);
      } else if (keyCode == KEY_DOWN && (modifiers & MODIFIER_ALT) != 0
          || keyCode == Util.KEY_DROPDOWN) {
        fireSearchStart(this);
      }
    }
  }

  public void onKeyPress(Widget sender, char keyCode, int modifiers) {
  }

  public void onKeyUp(Widget sender, char keyCode, int modifiers) {
  }

  // ClickListener for search button
  public void onClick(Widget sender) {
    if (sender == searchButton) {
      fireSearchStart(this);
    }
  }

  // ChangeListener for codeBox

  public void onChange(Widget sender) {
    if (sender == codeBox) {
//    fireChange();
      fireCodeChange(this);
    }
  }

  // ////////////////////////////////////////////

  ArrayList cnbListeners = null;

  public void addCNBListener(CNBListener listener) {
    if (cnbListeners == null)
      cnbListeners = new ArrayList();
    cnbListeners.add(listener);
  }

  public void removeCNBListener(CNBListener listener) {
    if (cnbListeners != null)
      cnbListeners.remove(listener);
  }

  public void fireCodeChange(CodeNameBox codeNameBox) {
    if (cnbListeners != null)
      for (int i = 0; i < cnbListeners.size(); ++i) {
        ((CNBListener) cnbListeners.get(i)).onCodeChanged(codeNameBox);
      }
  }

  public void fireSearchStart(CodeNameBox codeNameBox) {
    if (cnbListeners != null)
      for (int i = 0; i < cnbListeners.size(); ++i) {
        ((CNBListener) cnbListeners.get(i)).onSearchStart(codeNameBox);
      }
  }

  // ////////////////////////////////////////////

  ChangeListenerCollection changeListeners;

  public void addChangeListener(ChangeListener listener) {
    if (changeListeners == null) {
      changeListeners = new ChangeListenerCollection();
    }
    changeListeners.add(listener);
  }

  public void removeChangeListener(ChangeListener listener) {
    if (changeListeners != null)
      changeListeners.remove(listener);
  }

  public void fireChange() {
    if (userDataUuid == null) { // 没有找到
      Util.focusTextBox(codeBox);
    }
    if (changeListeners != null)
      for (Iterator it = changeListeners.iterator(); it.hasNext();) {
        ChangeListener listener = (ChangeListener) it.next();
        listener.onChange(this);
      }
  }

}
